package com.xingkeduo.business.user.controller;

import com.xingkeduo.business.user.domain.ConsumerInviteDomain;
import com.xingkeduo.pojo.dto.OrderCraftsmanResponseDTO;
import com.xingkeduo.pojo.dto.ScanDTO;
import com.xingkeduo.pojo.query.ShareQO;
import com.xingkeduo.pojo.table.ShareTO;
import com.xingkeduo.pojo.utils.ResponseUtils;
import com.xingkeduo.util.pojo.exception.InputParamException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/7/10 16:51
 **/
@Slf4j
@RequestMapping("/consumer/invite")
@RestController
public class ConsumerInviteController {

    @Resource
    private ConsumerInviteDomain consumerInviteDomain;


    private static String errorCode = "-1";
    private static String succCode = "0";
    private static String authorizationCode = "9004";

    /**
     * 查询用户最后一次服务的信息
     *
     * @param userId:
     * @return : org.springframework.http.ResponseEntity
     * @author : gaozifeng
     * @date : 2019/7/16 10:15
     */
    @GetMapping("/lastService")
    public ResponseEntity lastService(@RequestParam Long userId) {
        try {
            return new ResponseEntity<>(ResponseUtils.getSuccess(consumerInviteDomain.lastService(userId), succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.getSuccess(new OrderCraftsmanResponseDTO().setConsumerId(String.valueOf(userId))
                    .setCraftsmanId("0").setOrderNo("").setShopId("0")), HttpStatus.OK);
        }
    }

    /**
     * 分享到第三方并生成记录
     *
     * @param shareTO:
     * @return : 表id
     * @author : gaozifeng
     * @date : 2019/7/10 17:40
     */
    @GetMapping("/thirds")
    public ResponseEntity cretaShare(ShareTO shareTO) {
        try {
            return new ResponseEntity<>(ResponseUtils.getSuccess(consumerInviteDomain.createShare(shareTO), succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }

    }

    /**
     * 用户分享成功回调(微信回调废弃)
     *
     * @param shareQO:
     * @return : org.springframework.http.ResponseEntity
     * @author : gaozifeng
     * @date : 2019/7/11 11:55
     */
    @PostMapping("/shareRedirect")
    public ResponseEntity shareRedirect(@RequestBody ShareQO shareQO) {
        try {
            consumerInviteDomain.shareRedirect(shareQO);
            return new ResponseEntity<>(ResponseUtils.getSuccess(succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }
    }

    /**
     * 生成临时二维码链接
     *
     * @param shareId:分享表id 作为场景值
     * @return : org.springframework.http.ResponseEntity
     * @author : gaozifeng
     * @date : 2019/7/11 11:03
     */
    @RequestMapping("/initTempCode")
    public ResponseEntity initTempCode(Long shareId, Long userId) {
        try {
            return new ResponseEntity<>(ResponseUtils.getSuccess(consumerInviteDomain.getTempQrCode(shareId, userId), succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }
    }

    /**
     * 领取记录
     *
     * @param scanDTO:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/7/11 16:41
     */
    @GetMapping("/receive")
    public ResponseEntity newConsumerReceive(ScanDTO scanDTO) {
        try {
            consumerInviteDomain.userInviteReceiveCouponService(scanDTO);
            return new ResponseEntity<>(ResponseUtils.getSuccess(succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }
    }

    /**
     * 根据分享场景 获取用户头像
     *
     * @param shareId:
     * @return : org.springframework.http.ResponseEntity
     * @author : gaozifeng
     * @date : 2019/7/12 16:11
     */
    @RequestMapping("/headPortrait")
    public ResponseEntity getConsumerFileBySceneId(Long shareId) {
        try {
            return new ResponseEntity<>(ResponseUtils.getSuccess(consumerInviteDomain.getConsumerFileBySceneId(shareId), succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            if (t instanceof InputParamException) {
                return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), authorizationCode), HttpStatus.OK);
            }
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }
    }


    /**
     * 获取临时二维码记录 没有写到wiki 暂时无用
     *
     * @param id:
     * @return : org.springframework.http.ResponseEntity
     * @author : gaozifeng
     * @date : 2019/7/11 17:59
     */
    @GetMapping("/wechatQr")
    public ResponseEntity getWechatQrCodeInfoById(Long id) {
        try {
            return new ResponseEntity<>(ResponseUtils.getSuccess(consumerInviteDomain.getWechatQrCodeInfoById(id), succCode), HttpStatus.OK);
        } catch (Throwable t) {
            log.info("操作异常msg[{}],cause[{}]", t.getMessage(), t.getCause());
            return new ResponseEntity<>(ResponseUtils.error(t.getMessage(), errorCode), HttpStatus.OK);
        }
    }
}
